const userModel = require('../model/userModel.js')
const common = require('../model/common.js')
//用户注册
async function register(ctx) {
    let userName = ctx.request.body.username
    let password = ctx.request.body.password
    let mobile = ctx.request.body.mobile

    let res = await userModel.register({ userName, password, mobile })
    ctx.body = res
}
//用户登录
async function login(ctx) {
    let mobile = ctx.request.body.mobile
    let password = ctx.request.body.password
    let regx = /^1[356789]\d{9}$/

    if (!regx.test(mobile)) {
        return ctx.body = { code: 0, msg: '手机号格式有误', data: '' }
    }

    if (!password || password.length < 6 || password.length > 16) {
        return ctx.body = { code: 0, msg: '密码的长度应在6-16个字符之间', data: '' }
    }

    let [mobileExist] = await common.mobileExist(mobile)
    if (!mobileExist) {
        return ctx.body = { code: 0, msg: '手机号不存在', data: '' }
    }

    let repwd = await common.repassword(password, mobileExist)
    if (!repwd) {
        return ctx.body = { code: 0, msg: '密码有误', data: '' }
    }

    let token = await userModel.login(mobileExist)

    ctx.body = { code: 1, msg: '登陆成功', data: token }

}
//用户修改
async function mod(ctx) {
    let id = ctx.user.id
    let password = ctx.request.body.password
    let newPassword = ctx.request.body.newpassword
    let newUserName = ctx.request.body.username

    if (newUserName) {
        if (!newUserName || newUserName.length < 2 || newUserName.length > 10) {
            return ctx.body = { code: 0, msg: '昵称的长度应在2-10个字符之间', data: '' }
        }

        let userNameExist = await common.userNameExist(newUserName)
        if (userNameExist.length) {
            return ctx.body = { code: 0, msg: '用户名已存在', data: '' }
        }

        let res = await userModel.modUserName({ id, newUserName })
        return ctx.body = res


    }

    let userinfo = await common.devuserinfo(id)

    let repassword = await common.repassword(password, userinfo)
    if (!repassword) {
        return ctx.body = { code: 0, msg: '原密码有误', data: '' }
    }

    if (await common.repassword(newPassword, userinfo)) {
        return ctx.body = { code: 0, msg: '新密码不能与原密码相同', data: '' }
    }


    if (newPassword) {
        if (!newPassword || newPassword.length < 6 || newPassword.length > 16) {
            return ctx.body = { code: 0, msg: '密码的长度应在6-16个字符之间', data: '' }
        }

        let res = await userModel.modPassword({ id, newPassword })
        return ctx.body = res
    }

}
//当前用户信息
async function userinfo(ctx) {
    let id = ctx.user.id
    let res = await common.userinfo(id)
    ctx.body = { code: 1, msg: '成功', data: res }
}

//当前用户是否有支付密码
async function userpay(ctx) {
    let id = ctx.user.id
    let res = await common.userpay(id)
    if (res.paypassword == null) {
        ctx.body = { code: 1, msg: '您还没有支付密码，请先前往用户页设置支付密码', data: res }
    }else{
        ctx.body = true
    }
}

//验证支付密码
async function pay(ctx) {
    let paypassword = ctx.request.body.paypassword
    let id = ctx.user.id
    let user = await common.devuserinfo(id)
    console.log(user);
    let paypwd = await common.paypassword(paypassword, user)
    if (!paypwd) {
        return ctx.body = { code: 0, msg: '您输入的支付密码有误', data: '' }
    }
    ctx.body = { code: 1, msg: '支付成功', data: '' }
}

const db = require('../tool/db.js')
const bcrypt = require('bcrypt');
//修改支付密码
async function modpay(ctx) {
    let id = ctx.user.id
    let paypassword = ctx.request.body.paypassword
    let newpayPassword = ctx.request.body.newpaypassword
    let renewpayPassword = ctx.request.body.renewpaypassword
    let user = await common.devuserinfo(id)
    if (paypassword) {
        if (!paypassword || paypassword.length < 6 || paypassword.length > 16) {
            return ctx.body = { code: 0, msg: '密码的长度应在6-16个字符之间', data: '' }
        }
    }
    let paypwd = await common.paypassword(paypassword, user)
    if (!paypwd) {
        return ctx.body = { code: 0, msg: '您输入的支付密码有误', data: '' }
    }

    if (newpayPassword === paypassword) {
        return ctx.body = { code: 0, msg: '新密码不能与原密码相同', data: '' }
    }
    if (!(newpayPassword === renewpayPassword)) {
        return ctx.body = { code: 0, msg: '两次支付密码不同，请重新输入', data: '' }
    }

    newpayPassword = await bcrypt.hash(newpayPassword, 10);
    let [res] = await db.query('update user set paypassword = ? where id = ?', [newpayPassword, id])
    if (res.changedRows) {
        ctx.body = { code: 1, msg: '修改成功', data: '' }
    } else {
        ctx.body = { code: 1, msg: '修改失败，请重试', data: '' }
    }

}

async function paypwd(ctx){
    let paypassword = ctx.request.body.password
    let uid = ctx.user.id
    let reg = /^\d{6}$/
    if(!reg.test(paypassword)){
        return ctx.body = {code:0,msg:'密码格式错误'}
    }

    let res = await userModel.paypwd({paypassword,uid})
    ctx.body = res

}


module.exports = { register, login, mod, userinfo, userpay, pay, modpay,paypwd }